##############################################
#   PLOT IDEAL POINTS OF COMMITTEE AND AGENCIES
#   Figure 2
#	  JOSH CLINTON
##############################################

library(foreign)
# Get ideal point estimates
ipe<-read.dta(file="IPE110Transformed.dta")
hmed<-median(ipe$txbar[ipe$insty==1],na.rm=TRUE)
smed<-median(ipe$txbar[ipe$insty==2],na.rm=TRUE)
hdmed<-median(ipe$txbar[ipe$insty==1 & ipe$partyy==100],na.rm=TRUE)
sdmed<-median(ipe$txbar[ipe$insty==2 & ipe$partyy==100],na.rm=TRUE)
hmean<-mean(ipe$txbar[ipe$insty==1],na.rm=TRUE)
smean<-mean(ipe$txbar[ipe$insty==2],na.rm=TRUE)
hdmean<-mean(ipe$txbar[ipe$insty==1 & ipe$partyy==100],na.rm=TRUE)
sdmean<-mean(ipe$txbar[ipe$insty==2 & ipe$partyy==100],na.rm=TRUE)
bush<-1.25


#	110th Congress
#	All Mentioned Agencies

AgencyComm<-read.delim(file="AgencyCongress110FINAL.raw")

poo<-table(AgencyComm$agency)
agnames<-names(poo)
agnames<-agnames[-1]
agnames<-agnames[-12]	#	drop EOP
agnames<-agnames[-24]	#	drop OTH

goo<-rep(NA,times=length(agnames))
for(i in 1:length(agnames)){
  goo[i]<-mean(AgencyComm$cagentmean[AgencyComm$agency==agnames[i]])	}

isort<-sort(goo,index.return=TRUE)


agnames.sort<-agnames[isort$ix]	

pdf(file="AgencyCommittee110AllMedianCareerist.pdf")
plot(c(-1.5,1),c(1,32),type="n",axes=FALSE,xlab="Median Ideal Point of Agencies, Committees, Chambers and Parties",ylab="")

#polygon(c(-.17,-.17,.41,.41),c(-1,37,37,-1),col="Grey",border=NA)

axis(1)
axis(3)
text(rep(-1.5,times=30),seq(1,30),agnames.sort,pos=4,cex=.8)

for(i in 1:length(agnames.sort)){
  segments(-1.25,i,1.5,i)
  goo<-AgencyComm[AgencyComm$agency==agnames.sort[i],]
  
  segments(goo$cagentmean-1.96*goo$cagentmeanse,i,goo$cagentmean+1.96*goo$cagentmeanse,i,col="GREY",lwd=4)
  
  hsplotchar<-rep(1,times=dim(goo)[1])
  hsplotchar[goo$senate==1]<-6
  
  points(goo$cagentmean[1],i,pch=5,cex=1.5)
  for(j in 1:nrow(goo)){
    points(goo$commdn[j],i,pch=16)
  }
}

abline(v=hmed)	#	House Median
abline(v=smed)	#	Senate Median
text(smed,33,"Senate",pos=1)
text(hmed,33,"House",pos=1)

abline(v=hdmed)	#	House Median
abline(v=sdmed)	#	Senate Median
text(hdmed,32,"H Dem",pos=1)
text(sdmed,32,"S Dem",pos=1)

dev.off()

